<template>
  <div class="channel-edit">
   
    <van-cell title="我的频道" :border="false" center>
       <div slot="title" class="channel-title">我的频道</div>
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit=!isEdit"
      >{{isEdit?'完成':'编辑'}}</van-button>
    </van-cell>
    <van-grid :gutter="10">
     <van-grid-item
        :border="false"
        class="channel-item"
        :class="{active:index===active}"
        v-for="(channel,index) in userChannels"
        :key="index"
        :text="channel.name"
        :icon="(isEdit&&index !== 0) ? 'clear':'' "
        @click="onUserChannelClick(channel,index)"
      />
    </van-grid>

    <van-cell title="频道推荐" :border="false">
      <div slot="title" class="channel-title">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(channel,index) in recommendChannels"
        :key="index"
        :text="channel.name"
        @click="onAdd(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import {getAllChannels,addUserChannel,deleteUserChannel} from "@/api/channel"
import { mapState } from "vuex"
import {setItem} from '@/utils/storage'

export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    userChannels:
    {
      type:Array,
      required:true
    },
    active:{
      type:Number,
      required:true
    }
    },
  data () {
    return {
      allChannels:[],  //所有频道数据
      isEdit:false
      
    }
  },
  computed: {

    ...mapState(['user']),
    //推荐频道列表
    recommendChannels(){
      //所有频道-我的频道=剩下频道
   return  this.allChannels.filter(channel=>{
        //判断频道是否属于用户频道
       return !this.userChannels.find(userChannel=>{
          return userChannel.id===channel.id
        })

      })
       
    }

  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels()
    {
        const {data}= await getAllChannels()
        this.allChannels=data.data.channels
        //console.log(data)
    },
   async onAdd(channel){

      this.userChannels.push(channel)
      if(this.user){
        //登录，存储到线上
        
      await addUserChannel({
       channel: [
         {
        id: channel.id, // 频道 id
        seq1: this.userChannels.length // 频道的 序号
        }
      ]
      })
      }else{
        //没有登录储到到本地
        setItem('user-channels',this.userChannels)
      }
    },
    onUserChannelClick(channel,index){
      //如果是编辑状态删除
       if(this.isEdit&&index!==0){
          this.deleteChannel(channel,index)
       }
       else{
       //非编辑状态切换频道
         this.switchChannel(index)
         //切换频道
         this.$emit('update-active',index)
         //关闭弹出层
         this.$emit('close')
       }
    },
    //
  async  deleteChannel(channel,index){
      //如果删除的是当前激活频之前的频道
      if(index<=this.active){
        //更新激活频道的索引
        this.$emit('update-active',this.active-1)
      }
      this.userChannels.splice(index,1)  
      //数据持久化
      if(this.user){
           await deleteUserChannel(channel.id)
      }
      else{
         //没有登录储到到本地
        setItem('user-channels',this.userChannels)
      }
      
    },
    switchChannel(index){

    }

  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding-top: 54px;
  .channel-title{
    font-size:16px;
    color: #333333;

  }
  .channel-item {
    height: 43px;
    width: 80px;
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text {
        color: #222;
        font-size: 14px;
        margin-top: 0;
      }
    }
  }
  /deep/.van-grid-item__icon{
    color: #ccc;
    position: absolute;
    right: -5px;
    top: -5px;
    font-size: 20px;
    z-index: 2;
  }
  .active{
  /deep/.van-grid-item__text{
    color: red !important;;
  }
  }
}
</style>